package org.kl.wx.service;

import java.util.List;
import java.util.Map;

import me.chanjar.weixin.mp.api.WxMpService;

import org.apache.commons.lang3.StringUtils;
import org.kl.bf.utils.CRUDUtils;
import org.kl.bf.utils.KLDynamicSpecifications;
import org.kl.bf.utils.KLSearchFilter;
import org.kl.wx.entity.App;
import org.kl.wx.entity.Media;
import org.kl.wx.entity.MediaAsynInfo;
import org.kl.wx.repository.MediaAsynInfoDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component
@Transactional
public class MediaAsynInfoService {

	private MediaAsynInfoDao mediaAsynInfoDao;

	public MediaAsynInfo getMediaAsynInfo(String id) {
		return mediaAsynInfoDao.find(MediaAsynInfo.class, id);
	}

	public void saveMediaAsynInfo(MediaAsynInfo entity) {
		mediaAsynInfoDao.save(entity);
	}

	public void deleteMediaAsynInfo(String id) {
		mediaAsynInfoDao.delete(id);
	}

	public List<MediaAsynInfo> getAllMediaAsynInfo() {
		return mediaAsynInfoDao.findAll();
	}

	public String multiDelete(String ids) {
		String[] idArray = StringUtils.split(ids, ",");
		int i = 0;
		for (String id : idArray) {
			if (StringUtils.isNotBlank(id)) {
				mediaAsynInfoDao.delete(id);
				i++;
			}
		}
		return "成功删除" + i + "条记录";
	}
	
	public String deleteMai(WxMpService wxMpService,MediaAsynInfo mediaAsynInfo){
		//App app = mediaAsynInfo.getApp();
		try {
			//wxMpService.
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "";
	}

	public void saveMediaAsynInfo(App app, String mediaId, Media media) {
		MediaAsynInfo mediaAsynInfo = new MediaAsynInfo();
		mediaAsynInfo.setApp(app);
		mediaAsynInfo.setMedia(media);
		mediaAsynInfo.setMediaId(mediaId);
		mediaAsynInfoDao.save(mediaAsynInfo);
	}

	/**
	 * 按条件查询
	 */
	public List<MediaAsynInfo> getMediaAsynInfo(Map<String, Object> searchParams) {
		Specification<MediaAsynInfo> spec = buildSpecification(searchParams);
		return mediaAsynInfoDao.findAll(spec);
	}

	/**
	 * 按条件分页查询
	 */
	public Page<MediaAsynInfo> getMediaAsynInfo(Map<String, Object> searchParams, int pageNumber, int pageSize, String sortType,
			String orderType) {
		PageRequest pageRequest = CRUDUtils.buildPageRequest(pageNumber, pageSize, sortType, orderType);
		Specification<MediaAsynInfo> spec = buildSpecification(searchParams);

		return mediaAsynInfoDao.findAll(spec, pageRequest);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<MediaAsynInfo> buildSpecification(Map<String, Object> searchParams) {
		Map<String, KLSearchFilter> filters = KLSearchFilter.parse(searchParams);
		Specification<MediaAsynInfo> spec = KLDynamicSpecifications.byKLSearchFilter(filters.values(), MediaAsynInfo.class);
		return spec;
	}

	@Autowired
	public void setMediaAsynInfoDao(MediaAsynInfoDao mediaAsynInfoDao) {
		this.mediaAsynInfoDao = mediaAsynInfoDao;
	}

}
